<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\index\controller;
use think\Session;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Index extends Home
{
    public function index(){

        return $this->fetch();

    }

    //首页提交方法
    public function enrol(){

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if(empty($data)){
                $this->error('请填写信息');
            }
            if(empty($data['mobile'])){
                $this->error('请填写电话');
            }

            $mobileOr = db('user')->where('mobile',$data['mobile'])->find();

            if(!$mobileOr){
                $data['create_time'] = time();
                $r = db('user')->insert($data);
                if(!$r){
                    $this->error('操作失败，请重试');
                }
            }
            $uid = db('user')->where('mobile',$data['mobile'])->value("id");
            session('user_id', $uid);
            $this->success('提交成功',url('qrcode'));
        }

    }

    //二维码
    public function qrcode(){

        return $this->fetch();

    }

    //获取账号密码
    public function password(){

        if(!session('user_id')){
            $this->error('请重新填写个人信息');
        }
        $uid = session('user_id');

        //已有账号
        $passOr = db('account_psw')->where('acid',$uid)->find();
        if($passOr){
            return json($passOr);
        }

        //没有账号
        $account = db('account_psw')->where('acid',0)->find();
        if($account){
            db('account_psw')->where('id',$account['id'])->update(['acid'=>$uid]);
            return json($account);
        }else{
            return 2;
        }

    }

}
